import java.util.ArrayList;
import java.util.List;

/**
 * Leetcode 349. Intersection of Two Arrays
 * https://leetcode.com/problems/intersection-of-two-arrays/description/
 *
 * @author Kevin
 * @date 2019-01-01
 */
public class Solution349 {

    public static void main(String[] args) {
        int[] nums1 = {4, 9, 5};
        int[] nums2 = {9, 4, 9, 8, 4};

        int[] result = intersection(nums1, nums2);
        for (int item : result) {
            System.out.println(item);
        }
    }

    public static int[] intersection(int[] nums1, int[] nums2) {

        Set<Integer> set = new AVLSet<>();
        for (int num : nums1) {
            set.add(num);
        }

        List<Integer> list = new ArrayList<>();
        for (int num : nums2) {
            if (set.contains(num)) {
                list.add(num);
                set.remove(num);
            }
        }

        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }
}
